<template>
    <div class="layout-route">
        <div class="card card-search">
            <el-form :model="queryParams" ref="queryForm" v-show="showSearch" inline label-width="100px">
                <el-form-item label="店铺" prop="shop_id">
                    <el-select v-model="queryParams.shop_id" placeholder="请选择店铺" clearable size="small" style="width: 240px">
                        <el-option v-for="(dict, index) in shopList" :key="index" :label="dict.shop_name" :value="dict.shop_id" />
                    </el-select>
                </el-form-item>
                <el-form-item label="月份" prop="date">
                    <el-date-picker type="monthrange" v-model="queryParams.date" start-placeholder="开始时间" end-placeholder="结束时间"></el-date-picker>
                </el-form-item>
                 <el-form-item label=" ">
                    <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
                    <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="card">
            <el-row :gutter="10" class="total-warp">
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">原始销售额</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.total_price_old }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">原始单量</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.count }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">发货销售额</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.total_price }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">发货单量</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.count_LogisticsMarkupFee }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">总物流</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.freight }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">总采购</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.material }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">总争议</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.dispute }}</span>
                        </div>
                    </el-card>
                </el-col>
                <el-col :md="6" :sm="24" >
                    <el-card shadow="hover" class="text-large chart-card">
                        <div class="chart-card-header text-mini" slot="header">总物流加成</div>
                        <div class="chart-card-content">
                            <span class="content-title">{{ spends.LogisticsMarkupFee }}</span>
                        </div>
                    </el-card>
                </el-col>
            </el-row>
            <el-row :gutter="10" class="mb8">
                <!-- <el-col :span="1.5">
                    <el-button type="warning" plain icon="el-icon-download" :loading="download" :disabled="download" @click="handleExport">导出</el-button>
                </el-col> -->
                <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
            </el-row>
            <el-table v-loading="loading" :data="tableData" border script>
                <el-table-column label="店铺" prop="shop_name" min-width="140"></el-table-column>
                <el-table-column label="月份" prop="date" min-width="100"></el-table-column>
                <el-table-column label="原始销售额$" prop="total_price_old" min-width="100"></el-table-column>
                <el-table-column label="原始单量" prop="count" min-width="100"></el-table-column>

                <el-table-column label="发货销售额$" prop="total_price" min-width="100"></el-table-column>
                <el-table-column label="发货单量" prop="count_LogisticsMarkupFee" min-width="100"></el-table-column>
                <el-table-column label="发货件数" prop="skuquantity" min-width="100"></el-table-column>
                <el-table-column label="物流成本$" prop="freight" min-width="100"></el-table-column>
                <el-table-column label="采购成本$" prop="material" min-width="100"></el-table-column>
                <el-table-column label="争议成本$" prop="dispute" min-width="100"></el-table-column>
                <el-table-column label="物流加成$" prop="LogisticsMarkupFee" min-width="100"></el-table-column>
                <el-table-column label="操作" fixed="right">
                    <template slot-scope="scope">
                        <el-button type="text" icon="el-icon-download" @click="handleExport(scope.row)">导出</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <pagination v-show="total > 0" :total="total" :page.sync="pageInfo.page" :limit.sync="pageInfo.limit" @pagination="getList"></pagination>
        </div>
    </div>
</template>

<script>
import { queryMonthlyProfitList, getShopList, exportMonthlyProfitList } from '@/api/shop'
import { exportFile } from '@/utils/index'
import { parseTime } from '@/utils/quickadmin'
export default {
    name: 'MonthlyProfitList',
    data() {
        return {
            // 遮罩层
            loading: true,
            download: false,
            // 显示搜索条件
            showSearch: true,
            // 总条数
            total: 0,
            // 表格数据
            tableData: [],
            spends: {
                total_price_old: 0,
                count: 0,
                total_price: 0,
                count_LogisticsMarkupFee: 0,
                freight: 0,
                material: 0,
                dispute: 0,
                LogisticsMarkupFee: 0,
            },
            // 店铺列表下拉
            shopList: [],
            siteList: [],
            pageInfo: {
                page: 1,
                limit: 10,
            },
            // 查询参数
            queryParams: {
                shop_id: undefined,
                date: [],
            },
        }
    },
    async created() {
        this.loading = true
        try {
            const shopData = await getShopList()
            this.shopList = shopData?.data?.list ?? []
        } catch (error) {
            this.loading = false
            console.log(error)
        }
        this.getList()
    },
    methods: {
        handleExport(row) {
            this.download = true
            // const qyparams = {
            //     ...this.pageInfo,
            //     ...this.queryParams,
            // }
            // if (qyparams.date?.length == 2) {
            //     qyparams.date = [parseTime(qyparams.date[0], 'y-m'), parseTime(qyparams.date[1], 'y-m')]
            // }
            const qyparams = {
                ...row,
                // date: [row.date]
            }
            exportMonthlyProfitList(qyparams)
                .then(res => {
                    exportFile(res, () => {
                        this.download = false
                    })
                })
                .catch(err => {
                    this.download = false
                })
        },
                //列表
        getList() {
            this.loading = true
            const qyparams = {
                ...this.pageInfo,
                ...this.queryParams,
            }
            if (qyparams.date?.length == 2) {
                qyparams.date = [parseTime(qyparams.date[0], 'y-m'), parseTime(qyparams.date[1], 'y-m')]
            }
            queryMonthlyProfitList(qyparams)
                .then(response => {
                    this.tableData = response.data.list
                    this.spends = response.data.spends
                    this.total = response.data.total
                })
                .finally(() => {
                    this.loading = false
                })
        },

        // 搜索按钮操作
        handleQuery() {
            this.pageInfo.page = 1
            this.getList()
        },

        // 重置按钮操作
        resetQuery() {
            this.resetForm('queryForm')

            this.handleQuery()
        },
    },
}
</script>
